package com.ayla.demo.widgets.binder

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.ayla.demo.widgets.adapter.BinderAdapter
import com.ayla.demo.widgets.holder.BinderViewHolder
import com.ayla.demo.widgets.touch.IBinderTouch
import com.ayla.demo.widgets.touch.ITouch

abstract class AbsItemBinder<T, VB : ViewBinding>: IBinderTouch<VB> {

    internal var _adapter: BinderAdapter? = null

    val adapter: BinderAdapter
        get() {
            checkNotNull(_adapter) {
                "Binder没有与Adapter绑定, Adapter为空， 应该先调用addItemBinder()"
            }
            return _adapter!!
        }

    internal var _context: Context? = null

    val context: Context
        get() {
            checkNotNull(_context) {
                "Binder没有与Adapter绑定, Context为空"
            }
            return _context!!
        }



    fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BinderViewHolder<VB> {
        return BinderViewHolder(
            onCreateViewBinding(
                parent,
                LayoutInflater.from(parent.context),
                viewType
            )
        )
    }

    /**
     * 创建item的ViewBinder
     * @param parent
     * @param inflater
     * @param viewType
     * @return
     */
    abstract fun onCreateViewBinding(parent: ViewGroup, inflater: LayoutInflater, viewType: Int): VB

    private fun RecyclerView.ViewHolder.toBindViewHolder(): BinderViewHolder<VB> {
        return this as BinderViewHolder<VB>
    }

    fun convert(holder: RecyclerView.ViewHolder, data: T) {
        convert(holder.toBindViewHolder(), data)
    }

    /**
     * Item数据填充
     * @param holder
     * @param data
     */
    abstract fun convert(holder: BinderViewHolder<VB>, data: T)

    fun convert(holder: RecyclerView.ViewHolder, data: T, payload: List<Any>) {
        convert(holder.toBindViewHolder(), data, payload)
    }

    /**
     * Item数据填充，局部刷新
     * @param holder
     * @param data
     * @param payload
     */
    open fun convert(holder: BinderViewHolder<VB>, data: T, payload: List<Any>) {}

    fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
        onViewAttachedToWindow(holder.toBindViewHolder())
    }

    fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
        onViewDetachedFromWindow(holder.toBindViewHolder())
    }

    /**
     * 当该Binder出现在屏幕时
     * @param holder
     */
    open fun onViewAttachedToWindow(holder: BinderViewHolder<VB>) {}

    /**
     * 当该Binder从屏幕移除时
     * @param holder
     */
    open fun onViewDetachedFromWindow(holder: BinderViewHolder<VB>) {}

    /**
     * 整个Item的点击事件
     * @param holder
     * @param view
     * @param data
     * @param position
     */
    open fun onClick(
        holder: BinderViewHolder<out ViewBinding>,
        view: View,
        data: T,
        position: Int
    ) {
    }

    /**
     * ViewHolder已经创建完成，
     * 也意味着ViewBinding已经创建完成，已经Inflater
     */
    fun onViewHolderCreated(holder: RecyclerView.ViewHolder, viewType: Int) {
        onViewHolderCreated(holder.toBindViewHolder(), viewType)
    }

    open fun onViewHolderCreated(holder: BinderViewHolder<VB>, viewType: Int) {}

    fun onDrag(holder: RecyclerView.ViewHolder) {
        onDrag(holder.toBindViewHolder())
    }

    override fun onDrag(holder: BinderViewHolder<VB>) {

    }

    fun onEnd(holder: RecyclerView.ViewHolder) {
        onEnd(holder.toBindViewHolder())
    }

    override fun onEnd(holder: BinderViewHolder<VB>) {

    }

}